/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.salary.util;

import com.yunhoo.salary.model.WageItem;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 *
 * @author Administrator
 */
public class SalaXmlParser {
    // 从文件读取XML，输入文件名，返回XML文档

    public Document read(String fileName) throws MalformedURLException, DocumentException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File(fileName));
        return document;
    }

    public Document readString(String str) throws Exception {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new ByteArrayInputStream(str.getBytes("UTF-8")));
        return document;
    }
    //得到Root节点

    public Element getRootElement(Document doc) {
        return doc.getRootElement();
    }

    public void parserXml(String str) throws Exception {
        try {
            Document document = readString(str);
            Element employees = document.getRootElement();
            for (Iterator i = employees.elementIterator(); i.hasNext();) {
                Element employee = (Element) i.next();
                for (Iterator j = employee.elementIterator(); j.hasNext();) {
                    Element node = (Element) j.next();
                }
            }
        } catch (DocumentException e) {
            System.out.println(e.getMessage());
        }
    }

    /**
     * 解析从数据库查询生成的XML为文件
     *
     */
    public List getPsnSalaByMonth(List<String> fields, Map salaItems, String xml) throws DocumentException, DocumentException {
        try {
            Document document = readString(xml);
            Element sala = document.getRootElement();
            List psnSala = new ArrayList();
            String text = "";
            List<WageItem> psnWage = new LinkedList<WageItem>();
            WageItem item = null;
            for (String field : fields) {
                item = new WageItem();
                Element memberElm = sala.element(field.trim());// "member"是节点名              
                if (memberElm != null) {
                    item.setItemEng(field);
                    item.setItemCn(salaItems.get(field).toString());
                    text = memberElm.getText();
                    item.setItemValue(text);

                }
                if (text == null) {
                    text = "";
                }
                psnSala.add(text);

                psnWage.add(item);
            }
            return psnSala;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public List<String> getFields(Map salaItems) {
        List<String> fields = new LinkedList<String>();
        for (Object field : salaItems.keySet()) {
            fields.add(field.toString());
        }
        return fields;
    }

    public LinkedList<String> getFieldLabel(List<String> fields, Map salaItems) {
        LinkedList<String> labels = new LinkedList<String>();
        for (String field : fields) {
            labels.add(salaItems.get(field).toString());
        }
        return labels;
    }

    public List<LinkedList> getPsnSalaLedger(List<String> fields, List<String> xmlFiles) {
        List<LinkedList> ledger = new LinkedList<LinkedList>();
        LinkedList monthSala = null;
        for (String xml : xmlFiles) {
            try {
                Document document = readString(xml);
                Element sala = document.getRootElement();
                monthSala = new LinkedList();
                for (String field : fields) {
                    Element memberElm = sala.element(field.trim());// "member"是节点名
                    System.out.println("field:" + field + "," + memberElm.getName());
                    if (memberElm.getText() != null) {
                        monthSala.add(memberElm.getText());
                    } else {
                        monthSala.add("");
                    }
                }
                ledger.add(monthSala);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return ledger;
    }
}
